package com.miya.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import static org.springframework.amqp.rabbit.connection.CachingConnectionFactory.ConfirmType.SIMPLE;

/**
 * rabbitmq配置
 *
 * @author Caixiaowei
 * @date 2022/08/08
 */
@Slf4j
@Configuration
public class RabbitMqConfig {

    @Bean(name = "cachingConnectionFactory")
    @Primary
    public CachingConnectionFactory cachingConnectionFactory(
            @Value("${spring.rabbitmq.addresses}") String addresses,
            @Value("${spring.rabbitmq.username}") String username,
            @Value("${spring.rabbitmq.password}") String password,
            @Value("${spring.rabbitmq.virtual-host}") String virtualHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(addresses);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        //    #确认消息已发送到交换机(Exchange)
        connectionFactory.setPublisherConfirmType(SIMPLE);
        //    #确认消息已发送到交换机(Exchange)
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;
    }

    @Bean(name = "rabbitTemplate")
    @Primary
    public RabbitTemplate rabbitTemplate(
            @Qualifier("cachingConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(
                new RabbitTemplate.ConfirmCallback() {
                    @Override
                    public void confirm(
                            CorrelationData correlationData, boolean ack, String cause) {
                        log.info("ConfirmCallback:     " + "相关数据：" + correlationData);
                        log.info("ConfirmCallback:     " + "确认情况：" + ack);
                        log.info("ConfirmCallback:     " + "原因：" + cause);
                    }
                });

        rabbitTemplate.setReturnsCallback(
                new RabbitTemplate.ReturnsCallback() {
                    @Override
                    public void returnedMessage(ReturnedMessage returnedMessage) {
                        log.info("ReturnCallback:     " + "消息：" + returnedMessage.getMessage());
                        log.info("ReturnCallback:     " + "回应码：" + returnedMessage.getReplyCode());
                        log.info("ReturnCallback:     " + "回应信息：" + returnedMessage.getReplyText());
                        log.info("ReturnCallback:     " + "交换机：" + returnedMessage.getExchange());
                        log.info("ReturnCallback:     " + "路由键：" + returnedMessage.getRoutingKey());
                    }
                });
        return rabbitTemplate;
    }
}
